package com.keepsport.keepsportapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keepsport.keepsportapi.dao.UserVipDao;
import com.keepsport.keepsportapi.dto.UserVipDto;
import com.keepsport.keepsportapi.entity.UserVip;
import com.keepsport.keepsportapi.service.intf.UserVipService;
import com.keepsport.keepsportapi.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 *
 @Author YK
 @create 2022/7/5 19:51
 */
@Service
public class UserVipServiceImpl implements UserVipService {
    @Resource
    private UserVipDao dao;
    @Override
    public R selectVip(int token) {
        return R.ok(dao.selectVip(token));
    }

    @Override
    public R update(int token) {
        QueryWrapper<UserVip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",token);
        UserVip userVip = dao.selectOne(queryWrapper);
        if (userVip!=null) {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            //String format = sdf.format(userVip.getSdate());
            Calendar ca = Calendar.getInstance();
            ca.setTime(userVip.getSdate());
            ca.add(Calendar.DATE, 30);// num为增加的天数，可以改变的
            Date time = ca.getTime();
            //String enddate = sdf.format(time);

            userVip.setFlag(1);
            userVip.setDay((int) ((time.getTime()-(new Date()).getTime())+30));
            userVip.setSdate(new Date());
            userVip.setEdate(time);
            return R.ok();
        } else {
            UserVip userVip1 = new UserVip(token);
            dao.updateById(userVip1);
            return R.ok();
        }

    }
}
